package rank
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	import utils.DisplayTool;
	import utils.OpenWindow;
	
	import global.GameState;
	
	public class RankItem extends Sprite
	{
		private var rankNumber:*;
		private var face:*;
		private var nameText:TextField;
		private var moneyText:TextField;
		private var bg:Bitmap;
		private var tweenX:*;
		private var tweenY:*;
		private var container:Sprite;
		private var homePage:String;
		
		private var RemoteHtmlImg:*;
		
		private var TweenProperty:*;
		
		public function RankItem()
		{
			this.mouseChildren = false;
			this.buttonMode = true;
			initClass();
			initListener();
			initContainer();
			initBg();
			initRank();
			initFace();
			initText();
			initTween();
		}
		
		private function initClass():void
		{
			RemoteHtmlImg = getDefinitionByName("view.components.RemoteHtmlImg");
			TweenProperty = getDefinitionByName("utils.animate.TweenProperty");
		}
		
		private function initContainer():void
		{
			container = new Sprite();
			container.mouseEnabled = false;
			container.x = -38;
			container.y = -68;
			addChild(container);
		}
		
		private function initTween():void
		{
			tweenX = new TweenProperty();
			tweenX.duration = .2;
			
			tweenY = new TweenProperty();
			tweenY.duration = .2;
		}
		
		public function update(data:XML):void
		{
			nameText.text = data.@name;
			moneyText.text = "$" + data.@gameMoney;
			homePage = data.@homePage;
			face.loadImg(data.@image);
		}
		
		public function set index(v:int):void
		{
			rankNumber.value = v;
		}
		
		private function initBg():void
		{
			bg = new Bitmap(RankTool.BgBitmapdata);
			container.addChild(bg);
		}
		
		private function initText():void
		{
			nameText = new TextField();
			moneyText = new TextField();
			
			var tf:TextFormat = new TextFormat();
			tf.size = 12;
			tf.color = 0xFFFFFF;
			tf.align = "center";
			nameText.defaultTextFormat = tf;
			
			tf.color = 0xFFFF00;
			tf.bold = true;
			
			moneyText.defaultTextFormat = tf;
			
			nameText.height = moneyText.height = 20;
			nameText.width = moneyText.width = 60;
			
			nameText.x = 10;
			moneyText.x = 10;
			nameText.y = 80;
			moneyText.y = nameText.y + 15;
			
			container.addChild(nameText);
			container.addChild(moneyText);
		}
		
		private function initFace():void
		{
			face = new RemoteHtmlImg(50,50);
			face.x = 12;
			face.y = 30;
			container.addChild(face);
		}
		
		private function initRank():void
		{
			rankNumber = new RankNumber();
			container.addChild(rankNumber);
		}
		
		private function initListener():void
		{
			addEventListener(MouseEvent.MOUSE_OVER,onOver);
			addEventListener(MouseEvent.MOUSE_OUT,onOut);
			addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function removeListener():void
		{
			removeEventListener(MouseEvent.MOUSE_OVER,onOver);
			removeEventListener(MouseEvent.MOUSE_OUT,onOut);
			removeEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(event:MouseEvent):void
		{
			if (homePage == "" || GameState.sns == "2")
			{
				return;
			}
			OpenWindow.openWindow(homePage);
		}
		
		private function onOut(event:MouseEvent):void
		{
			tweenX.stop();
			tweenX.setTarget(this,"scaleX",this.scaleX,1);
			tweenX.start();
			
			tweenY.stop();
			tweenY.setTarget(this,"scaleY",this.scaleX,1);
			tweenY.start();
		}
		
		private function onOver(event:MouseEvent):void
		{
			tweenX.stop();
			tweenX.setTarget(this,"scaleX",this.scaleX,1.05);
			tweenX.start();
			
			tweenY.stop();
			tweenY.setTarget(this,"scaleY",this.scaleX,1.05);
			tweenY.start();
		}
		
		public function clear():void
		{
			tweenY.stop();
			tweenX.stop();
			nameText.text = "";
			moneyText.text = "";
			face.clear();
		}
		
		public function destroy():void
		{
			DisplayTool.removeAllChild(this);
			DisplayTool.removeAllChild(container);
			tweenY.destroy();
			tweenX.destroy();
			tweenX = null;
			tweenY = null;
			
			removeListener();
			rankNumber.clear();
			rankNumber = null;
			face.destroy();
			face = null;
			nameText = null;
			moneyText = null;
			bg.bitmapData = null;
			bg = null;
			container = null;
		}
	}
}